package com.wu.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wu.error.BusinessException;
import com.wu.error.EnumBusinessError;
import com.wu.mapper.ArticleReportMapper;
import com.wu.pojo.ArticleReport;
import com.wu.service.ArticleReportService;
import com.wu.service.ArticlesService;
import com.wu.util.MyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.wu.constant.Constant.*;

/**
 * @author: 吴磊
 * @program: blog
 * @create: 2021-05-12 16:44
 */
@Service
public class ArticleReportServiceImpl extends ServiceImpl<ArticleReportMapper, ArticleReport> implements ArticleReportService {

    @Autowired
    private ArticlesService articlesService;


    @Autowired
    private MyUtil myUtil;

    @Override
    public void insertArticleReport(String articleId, String articleTitle, String reportReason, String added) {
        String reportDate = myUtil.getCurrentDetailedTime();
        ArticleReport articleReport = new ArticleReport();
        articleReport.setArticleId(articleId);
        articleReport.setArticleTitle(articleTitle);
        articleReport.setReportReason(reportReason);
        articleReport.setAdded(added);
        articleReport.setReportDate(reportDate);
        int integer = baseMapper.insert(articleReport);
        if (integer <= 0) {
            throw new BusinessException(EnumBusinessError.UNKNOWN_ERROR, "举报失败");
        }
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void deleteArticleReportById(String reportId, String type) {
        if (!StringUtils.isEmpty(type)) {
            ArticleReport articleReport = baseMapper.getArticleReportById(reportId);
            String articleId = articleReport.getArticleId();
            articlesService.deleteArticleByArticleId(articleId);
        } else {
            baseMapper.deleteArticleReportById(reportId);
        }
    }

    @Override
    public Map<String, Object> getAllArticleReport(Integer curPage) {
        Map<String, Object> map = new HashMap<>();
        Integer count = baseMapper.getAllArticleReportCount();
        map.put(COUNT, count);
        int totalPage = (count + 15 - 1) / 15;
        if (totalPage == 0) {
            map.put(TOTAL_PAGE, 0);
            map.put(ARTICLE_REPORTS, new ArrayList<>());
            return map;
        }
        if (curPage < 1) {
            curPage = 1;
        }
        if (curPage > totalPage) {
            curPage = totalPage;
        }
        map.put(TOTAL_PAGE, totalPage);
        List<ArticleReport> reportList = baseMapper.getAllArticleReport((curPage - 1) * 15);

        reportList = reportList.stream().map(report -> {
            String added = report.getAdded();
            if (!StringUtils.isEmpty(added)) {
                String s = added.substring(0, Math.min(added.length(), 50));
                report.setReportReason(s);
            }
            return report;
        }).collect(Collectors.toList());

        map.put(ARTICLE_REPORTS, reportList);
        return map;
    }

    @Override
    public Map<String, Object> getArticleReportById(String reportId) {
        ArticleReport report = baseMapper.getArticleReportById(reportId);
        Map<String, Object> map = new HashMap<>();
        map.put(ARTICLE_REPORTS, report);
        return map;
    }
}
